package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.model.TbBrand;
import com.dongdongshop.model.TbBrandExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.vo.BrandVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private TbBrandMapper tbm;

    @Override
    public PageResult<BrandVO> brandList(PageParms<BrandVO> parms) {
        BrandVO parm = parms.getParm();
        TbBrandExample te = new TbBrandExample();
        TbBrandExample.Criteria criteria = te.createCriteria();
        //判断name是否为空
        if (StringUtils.isNotBlank(parm.getName())){
            criteria.andNameLike("%"+parm.getName()+"%");
        }
        //判断firstChar是否为空
        if (StringUtils.isNotBlank(parm.getFirstChar())){
            criteria.andFirstCharEqualTo(parm.getFirstChar());
        }
        PageHelper.startPage(parms.getPageNum(), parms.getPageSize());
        List<TbBrand> tbBrandList = tbm.selectByExample(te);
        PageInfo info = new PageInfo(tbBrandList);
        List<BrandVO> voList = tbBrandList.stream().map(tb -> {
            BrandVO bvo = new BrandVO();
            BeanUtils.copyProperties(tb, bvo);
            return bvo;
        }).collect(Collectors.toList());
        return new PageResult<>(voList, info.getTotal(), parms.getPageNum(), parms.getPageSize());
    }

    @Override
    public void addBrand(BrandVO brandVO) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brandVO,tbBrand);
        tbm.insertSelective(tbBrand);
    }

    @Override
    public void updateBrand(BrandVO brandVO) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brandVO,tbBrand);
        tbm.updateByPrimaryKeySelective(tbBrand);
    }

    @Override
    public void deleteById(Long id) {
        tbm.deleteByPrimaryKey(id);
    }

    @Override
    public BrandVO selectById(Long id) {
        TbBrand tbBrand = tbm.selectByPrimaryKey(id);
        BrandVO brandVO = new BrandVO();
        BeanUtils.copyProperties(tbBrand,brandVO);
        return brandVO;
    }

    @Override
    public List<BrandVO> selectAll() {
        List<TbBrand> tbBrandList = tbm.selectByExample(null);
        List<BrandVO> voList = tbBrandList.stream().map(tb -> {
            BrandVO bvo = new BrandVO();
            BeanUtils.copyProperties(tb, bvo);
            return bvo;
        }).collect(Collectors.toList());
        return voList;
    }
}
